<?php

namespace Agp\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use APY\DataGridBundle\Grid\Mapping as GRID;

/**
 * Agp\CoreBundle\Entity\Terminal
 *
 * @ORM\Table(name="terminal")
 * @ORM\Entity
 * @GRID\Source(groupBy={"codTerminal"}, columns="codTerminal, noSerie, versaoSistema, situacao, terminalReference.empresa.codEmpresa")
 */
class Terminal
{
    /**
     * @var integer $codTerminal
     *
     * @ORM\Column(name="cod_terminal", type="integer", nullable=false)
     * @ORM\Id
     * @GRID\Column(title="Código", type="text", size="5", primary=true)
     */
    protected $codTerminal;
    
    ###################################################
    /**
     * @var string $atualizacao
     * 
     *
     * @ORM\OneToMany(targetEntity="Atualizacao", mappedBy="terminal", cascade={"persist"}, orphanRemoval=true)
     *
     */
    protected $atualizacao;
    
    /**
     * 
     *
     * @ORM\OneToMany(targetEntity="LogErroTerminal", mappedBy="terminal", cascade={"persist"}, orphanRemoval=true)
     *
     */
    protected $erros;
    ###################################################
    
    /**
     *
     * @ORM\OneToMany(targetEntity="TerminalEmpresa", mappedBy="terminal", cascade={"all"})
     * @ORM\OrderBy({"codTerminalEmpresa" = "ASC"})
     * 
     * @GRID\Column(field="terminalReference.empresa.codEmpresa", visible=false, type="text", title="Empresa")
     * @GRID\Column(field="terminalReference.empresa.razaoSocial", type="text", title="Empresa")
     */
    protected $terminalReference;

    /**
     * @var string $noSerie
     *
     * @ORM\Column(name="no_serie", type="string", length=11, nullable=false)
     * @Assert\NotBlank
     * 
     * @GRID\Column(title="Série", type="text")
     */
    protected $noSerie;

    
    const FRABRICANTE_VERIFONE = "Verifone";
    /**
     * @var string $fabricante
     *
     * @ORM\Column(name="fabricante", type="string", nullable=true)
     * @Assert\NotBlank
     */
    protected $fabricante;

    const VERSAO_VX520 = "VX520";
    const VERSAO_VX680 = "VX680";
    /**
     * @var string $versao
     *
     * @ORM\Column(name="versao", type="string", nullable=true)
     * @Assert\NotBlank
     */
    protected $versao;
    
    /**
     * @var string $versao
     *
     * @ORM\Column(name="versao_sistema", type="string", nullable=true)
     * @GRID\Column(title="Versão", type="text", align="center", filter="select", selectFrom="source")
     * @Assert\NotBlank
     */
    protected $versaoSistema;

    /**
     * @var \DateTime $dtEntrada
     *
     * @ORM\Column(name="dt_entrada", type="date", nullable=false)
     * @Assert\NotBlank
     */
    protected $dtEntrada;

    const SIT_DISPONIVEL = "Disponível";
    const SIT_MANUTENCAO = "Manutenção";
    const SIT_ALUGADO = "Alugado";
    const SIT_VENDIDO = "Vendido";
    /**
     * @var string $situacao
     *
     * @ORM\Column(name="situacao", type="string", nullable=true)
     * @Assert\NotBlank
     * 
     * @GRID\Column(title="Situação", type="text", align="center", filter="select", selectFrom="source")
     */
    protected $situacao;
    
    const PROP_AGAPE = "Agape";
    const PROP_EMPRESA = "Empresa";
    /**
     * @var string $propriedade
     *
     * @ORM\Column(name="propriedade", type="string", nullable=true)
     * @Assert\NotBlank
     */
    protected $propriedade;

    public function __construct() {
        $this->terminalReference = new ArrayCollection();
        $this->dtEntrada = new \DateTime;
        $this->chamadas = new ArrayCollection();
    }
    
    public function __toString() {
        return $this->getCodTerminal().' ('.$this->getNoSerie().')';
    }


    /* hack for Acls */
    public function getId(){
        return 'class';
    }

    /**
     * Get codTerminal
     *
     * @return integer 
     */
    public function getCodTerminal()
    {
        return str_pad($this->codTerminal, 4, "0", STR_PAD_LEFT);
    }
    
    public function getNoSerieSerial(){
        $serie = explode('-', $this->noSerie);
        $serie = implode('', $serie);
        return substr($serie, 2, 9);
    }

    public function getCodTerminalNoPad()
    {
        return $this->codTerminal;
    }
    
    public function setCodTerminal($codTerminal) {
        $this->codTerminal = $codTerminal;
    }    

    /**
     * Set noSerie
     *
     * @param string $noSerie
     * @return Terminal
     */
    public function setNoSerie($noSerie)
    {
        $this->noSerie = $noSerie;
    
        return $this;
    }

    /**
     * Get noSerie
     *
     * @return string 
     */
    public function getNoSerie()
    {
        return $this->noSerie;
    }

    /**
     * Set fabricante
     *
     * @param string $fabricante
     * @return Terminal
     */
    public function setFabricante($fabricante)
    {
        $this->fabricante = $fabricante;
    
        return $this;
    }

    /**
     * Get fabricante
     *
     * @return string 
     */
    public function getFabricante()
    {
        return $this->fabricante;
    }

    /**
     * Set versao
     *
     * @param string $versao
     * @return Terminal
     */
    public function setVersao($versao)
    {
        $this->versao = $versao;
    
        return $this;
    }

    /**
     * Get versao
     *
     * @return string 
     */
    public function getVersao()
    {
        return $this->versao;
    }

    /**
     * Set dtEntrada
     *
     * @param \DateTime $dtEntrada
     * @return Terminal
     */
    public function setDtEntrada($dtEntrada)
    {
        $this->dtEntrada = $dtEntrada;
    
        return $this;
    }

    /**
     * Get dtEntrada
     *
     * @return \DateTime 
     */
    public function getDtEntrada()
    {
        return $this->dtEntrada;
    }

    /**
     * Set situacao
     *
     * @param string $situacao
     * @return Terminal
     */
    public function setSituacao($situacao)
    {
        $this->situacao = $situacao;
    
        return $this;
    }

    /**
     * Get situacao
     *
     * @return string 
     */
    public function getSituacao()
    {
        return $this->situacao;
    }
    
    public function getPropriedade() {
        return $this->propriedade;
    }

    public function setPropriedade($propriedade) {
        $this->propriedade = $propriedade;
    }
    
      public function setChamadas($chamadas) {
        $this->chamadas = $chamadas;
    }
    
    public function getChamadas() {
        return $this->chamadas;
    }

    public function setAtualizacao($valor) {
        $this->atualizacao = $valor;
    }
    
    public function getAtualizacao() {
        return $this->atualizacao;
    }
    
    public function setErros($valor) {
        $this->erros = $valor;
    }
    
    public function getErros() {
        return $this->erros;
    }
 
    /**
     * Add $terminalReference
     *
     * @param Agp\CoreBundle\Entity\TerminalReference $terminalReference
     */
    public function addTerminalReference(\Agp\CoreBundle\Entity\TerminalEmpresa $terminalReference)
    {
        $this->terminalReference[] = $terminalReference;
    }

    /**
     * Get terminalReference
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getTerminalReference()
    {
        return $this->terminalReference;
    }
    
    public function getVersaoSistema() {
        return $this->versaoSistema;
    }

    public function setVersaoSistema($versaoSistema) {
        $this->versaoSistema = $versaoSistema;
    }

      
    static public function getFrabricanteList()
    {
        return array(self::FRABRICANTE_VERIFONE => self::FRABRICANTE_VERIFONE);
    }
    
    static public function getVersaoList()
    {
        return array(self::VERSAO_VX520 => self::VERSAO_VX520,
                     self::VERSAO_VX680 => self::VERSAO_VX680);
    }
    
    static public function getSituacaoList()
    {
        return array(self::SIT_DISPONIVEL => self::SIT_DISPONIVEL,
                     self::SIT_MANUTENCAO => self::SIT_MANUTENCAO,
                     self::SIT_ALUGADO => self::SIT_ALUGADO,
                     self::SIT_VENDIDO => self::SIT_VENDIDO);
    }
    
    static public function getPropriedadeList()
    {
        return array(self::PROP_AGAPE => self::PROP_AGAPE,
                     self::PROP_EMPRESA => self::PROP_EMPRESA);
    }
    
    public function getTerminalVinculoAtivo(){
        foreach ($this->getTerminalReference() as $terminalReference){
            if ($terminalReference->isAtivo()){
                return $terminalReference;
            }
        }
    }
}